﻿using Infrastructure.ActionResult;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace Microsoft.AspNetCore.Mvc
{
    public static class ApiResultExtensions
    {
        /// <summary>
        /// 成功
        /// </summary>
        /// <param name="controllerBase"></param>
        /// <param name="data"></param>
        /// <param name="msg"></param>
        /// <param name="hasMore"></param>
        /// <returns></returns>
        public static ApiReturn Success(this ControllerBase controllerBase, Object data, string msg = "成功", bool hasMore = false)
        {
            return controllerBase.DefaultResult(ResultCode.Sucess, data, msg, hasMore);
        }
        /// <summary>
        /// 发生错误
        /// </summary>
        /// <param name="controllerBase"></param>
        /// <param name="data"></param>
        /// <param name="msg"></param>
        /// <param name="hasMore"></param>
        /// <returns></returns>
        public static ApiReturn Error(this ControllerBase controllerBase, Object data, string msg = "失败", bool hasMore = false)
        {
            return controllerBase.DefaultResult(ResultCode.Error, data, msg, hasMore);
        }
        /// <summary>
        /// 失败
        /// </summary>
        /// <param name="controllerBase"></param>
        /// <param name="data"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static ApiReturn FailResult(this ControllerBase controllerBase, Object data, string msg = "失败")
        {
            return controllerBase.DefaultResult(ResultCode.Fail, data, msg);
        }
        /// <summary>
        /// 其他自定义码
        /// </summary>
        /// <param name="controllerBase"></param>
        /// <param name="code"></param>
        /// <param name="data"></param>
        /// <param name="msg"></param>
        /// <param name="hasMore"></param>
        /// <returns></returns>
        public static ApiReturn DefaultResult(this ControllerBase controllerBase, ResultCode code, Object data, string msg, bool hasMore = false)
        {
            return new ApiReturn(data, code, msg, hasMore);
        }

        public static AdminApiReturn AdminSuccess(this ControllerBase controllerBase, Object data, string msg = "请求成功", int count = 0)
        {
            return AdminDefaultResult(controllerBase,ResultCode.Sucess, data, msg, count);
        }
        public static AdminApiReturn AdminDefaultResult(this ControllerBase controllerBase, ResultCode code, Object data, string msg, int count = 0)
        {
            return new AdminApiReturn(data, code, msg, count);
        }


        [Serializable]
        public class ApiReturn : JsonResult
        {
            public ResultCode code { get; set; }
            public string msg { get; set; }
            public bool hasMore { get; set; }
            public object data { get; set; }
            public ApiReturn(object value, ResultCode code, string msg, bool hasMore) : base(new { code = code, data = value, msg = msg, hasMore = hasMore })
            {
                this.code = code;
                this.msg = msg;
                this.hasMore = hasMore;
                this.data = value;


            }

            internal ApiReturn(object obj) : base(obj)
            {

            }
        }

        [Serializable]
        public class AdminApiReturn : JsonResult
        {
            public ResultCode code { get; set; }
            public string msg { get; set; }
            public int count { get; set; }
            public object data { get; set; }

            public AdminApiReturn(object value, ResultCode code, string msg, int count) : base(new { code = code, data = value, msg = msg, count = count })
            {
                this.code = code;
                this.msg = msg;
                this.count = count;
                this.data = value;
            }

            internal AdminApiReturn(object obj) : base(obj)
            {

            }
           
        }

    }

    public enum ResultCode
    {
        /// <summary>
        /// 以后返回成功都用这个。
        /// </summary>
        Sucess = 0,
        /// <summary>
        /// 处理失败返回
        /// </summary>
        Fail = 999,
        /// <summary>
        /// 处理时发生错误
        /// </summary>
        Error = -1,
        NotRegister = 99
    }
}
